package com.gitee.feizns.explore.data_structure.tree.bs.easy;

/**
 * 938. 二叉搜索树的范围和
 * @author feizns
 * @since 2020/1/6
 */
public class RangeSumBST {

    public static void main(String[] args) {

    }

    static int sum = 0;

    public static int rangeSumBST(TreeNode root, int L, int R) {
        if ( root != null ) {
            if ( root.val < L )
                sum += rangeSumBST(root.right, L, R);
            else if ( root.val > R )
                sum += rangeSumBST(root.left, L, R);
            else {
                sum += root.val;
                sum += rangeSumBST(root.left, L, R);
                sum += rangeSumBST(root.right, L, R);
            }
        }
        return sum;
    }

    private static void inOrder(TreeNode node, int L, int R) {
        if ( node != null ) {
            inOrder(node.left, L, R);
            if ( node.val >= L && node.val <= R )
                sum += node.val;
            inOrder(node.right, L, R);
        }
    }

//    public static TreeNode create() {
//        TreeNode ret = new T
//        return null;
//    }

}
